package me.jiatao.wan.common.ui

import android.content.Context
import android.util.AttributeSet
import com.airbnb.lottie.LottieAnimationView

/**
 * @Author: taojia
 * @CreateDate: 2021/9/2 5:55 下午
 * @Version: 1.0
 * @Description:
 */

class LoadingLottieAnimation @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LottieAnimationView(context, attrs, defStyleAttr) {

    enum class State {
        NONE,
        LOADING,
        ERROR,
        COMPLETE
    }

    var status: State = State.NONE
        set(value) {
            /** 过滤重复操作 */
            if (field == value) {
                return
            }

            field = value
            when (value) {
                State.LOADING -> {
                    setMinAndMaxFrame(0, 90)
                    playAnimation()
                    visibility = VISIBLE
                }

                State.ERROR -> {
                    setMinAndMaxFrame(91, 192)
                    playAnimation()
                    visibility = VISIBLE
                }

                State.COMPLETE -> {
                    visibility = GONE
                    pauseAnimation()
                }
                else -> {}
            }
        }
}